package com.xlh.util;

import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @author cheer
 */
public class InputValidator {

    public static final String INVALID_INPUT = "只允许输入数字和字母";

    public static final String INPUT_VALIDATOR = "[0-9a-zA-Z]+";

    public static final String SPECIAL_CHAR = "['\\[\\]]|\n|\r|\t";

    private static final Pattern INPUT_PATTERN = Pattern.compile(INPUT_VALIDATOR);

    private static final Pattern SPECIAL_CHAR_PATTERN = Pattern.compile(SPECIAL_CHAR);

    public static boolean isValidInput(String input) {
        Matcher matcher = INPUT_PATTERN.matcher(input);
        return matcher.matches();
    }

    /**
     * 去掉特殊字符
     *
     * @param str
     * @return
     */
    public static String regSpecialChar(String str) {
        if (StringUtils.isEmpty(str)) return "";
        Matcher m = SPECIAL_CHAR_PATTERN.matcher(str);
        return m.replaceAll("").trim();
    }
}